/*
 * GMoveday.java
 * Created on 18 juin 07
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2007, Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.season;


import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.hockeo.vhbase.data.dao.IGEntity;

/**
 * Represents a move day.
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@Table( name = "m_moveday" )
public class GMoveday implements IGEntity, Comparable<GMoveday>
{
  @Id
  @Column( name = "id" )
  private int               d_nId;

  @Column( name = "date" )
  private GregorianCalendar d_calDate;

  @Column( name = "active" )
  private boolean           d_fActive;

  @Column( name = "reserved_league" )
  private boolean           d_fReservedLeague;

  @Column( name = "reserved_cup" )
  private boolean           d_fReservedCup;

  @Column( name = "reserved_eu1" )
  private boolean           d_fReservedEU1;

  @Column( name = "reserved_eu2" )
  private boolean           d_fReservedEU2;

  @Column( name = "prepared" )
  private boolean           d_fPrepared;

  @Column( name = "moves_made" )
  private boolean           d_fMovesMade;

  @Column( name = "executed" )
  private boolean           d_fExecuted;

  @OneToMany( mappedBy = "d_moveday", cascade = { CascadeType.REMOVE } )
  @OrderBy( "d_nId" )
  private List<GMatchday>   d_listMatchdays;

  /** Default parameterless constructor. */
  protected GMoveday()
  {}

  /**
   * Resets the moveday by setting various flags to their initial start of season state.
   * The moveday date is also reset to <code>null</code>.
   */
  public void reset()
  {
    d_calDate = null;
    d_fActive = false;
    d_fPrepared = false;
    d_fMovesMade = false;
    d_fExecuted = false;
  }

  /**
   * Attaches a new matchday to this moveday.
   *
   * @param matchday the matchday to be added to this moveday
   */
  public void addMatchday( GMatchday matchday )
  {
    if ( d_listMatchdays == null )
      d_listMatchdays = new ArrayList<GMatchday>();

    d_listMatchdays.add( matchday );

    // set moveday to matchday
    //
    matchday.setMoveday( this );
  }

  public List<GMatchday> getMatchdays()
  {
    return d_listMatchdays;
  }

  public GregorianCalendar getDate()
  {
    return d_calDate;
  }

  /**
   * Indicates whether this moveday is the currently active one (i.e. the one to be played
   * next). At any time, there is either only a single active moveday or none, if the
   * season is not yet prepared or already finished.
   */
  public boolean isActive()
  {
    return d_fActive;
  }

  public void setActive( boolean fActive )
  {
    d_fActive = fActive;
  }

  /**
   * Indicates whether this moveday is usable for national league competitions.
   */
  public boolean isReservedForLeague()
  {
    return d_fReservedLeague;
  }

  /**
   * Indicates whether this moveday is usable for national cup competitions.
   */
  public boolean isReservedForCup()
  {
    return d_fReservedCup;
  }

  /**
   * Indicates whether this moveday is usable for the top level intl. competition.
   */
  public boolean isReservedForEU1()
  {
    return d_fReservedEU1;
  }

  /**
   * Indicates whether this moveday is usable for the 2nd level intl. competition.
   */
  public boolean isReservedForEU2()
  {
    return d_fReservedEU2;
  }

  /**
   * Sets the executed flag that indicates when a moveday is fully finished. If the date
   * is not yet set, it will also be set by this method if the provided flag is
   * <code>true</code>.
   *
   * @param fExecuted whether the moveday should be marked as executed or not
   */
  public void setExecuted( boolean fExecuted )
  {
    d_fExecuted = fExecuted;

    if ( d_fExecuted && d_calDate == null )
      d_calDate = new GregorianCalendar();
  }

  public boolean isExecuted()
  {
    return d_fExecuted;
  }

  public boolean isPrepared()
  {
    return d_fPrepared;
  }

  public void setPrepared( boolean fPrepared )
  {
    d_fPrepared = fPrepared;
  }

  public boolean isMovesMade()
  {
    return d_fMovesMade;
  }

  public void setMovesMade( boolean fMovesMade )
  {
    d_fMovesMade = fMovesMade;
  }

  public int getId()
  {
    return d_nId;
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GMoveday.class;
  }

  public boolean instanceOf( Class<? extends IGEntity> classOther )
  {
    return classOther.isAssignableFrom( getGEntityClass() );
  }

  /*
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals( Object obj )
  {
    try
    {
      return d_nId == ( (GMoveday) obj ).getId();
    }
    catch ( Exception ex ) // especially ClassCastException(s) may happen
    {
      return false;
    }
  }

  /*
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return d_nId;
  }

  /*
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return "Moveday " + String.valueOf( d_nId );
  }

  /**
   * Ensures ordering by moveday id.
   */
  public int compareTo( GMoveday other )
  {
    return this.getId() - other.getId();
  }
}
